/**
 * @author harae Feng 
 * @date 2019-09-24 15:41:35 
 * @Last Modified by: harae Feng
 * @Last Modified time: 2019-09-24 16:15:36
 * @Describe 抖音-明星爱Dou榜
 */


const fetch = require('../../util/fetchUtil').fetchRequest
const ResultAPI = require('../../routes/api/resultAPI')
const CatchHandle = require('../../routes/api/apicatch')


/**
 * 明星爱Dou榜-实时
 */
async function starDouCurr(){
    let url = `https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/star/`
    let res = await fetch(url)
    let resJSON = await res.json()
    return resJSON
}

/**
 * 明星爱Dou榜-往期榜单
 * @param {String} edition_uid 时间周期榜单
 */
async function starDouHistory(edition_uid){
    let url = ` https://api-hl.amemv.com/aweme/v1/hotsearch/star/billboard/?version_code=8.0.0&pass-region=1&pass-route=1&js_sdk_version=1.17.4.3&app_name=aweme&vid=B42183A7-CB2D-4133-9549-08558A11D068&app_version=8.0.0&device_id=58500535096&channel=App%20Store&mcc_mnc=46001&aid=1128&screen_width=1242&openudid=25f978db609ac0ced8be42b1dd51ff0fd31e9ef6&os_api=18&ac=4G&os_version=12.1.4&device_platform=iphone&build_number=80015&device_type=iPhone9,2&iid=86581271591&idfa=5439BD54-8371-4465-901A-14B4442B5E08&type=1&edition_uid=${edition_uid}&request_tag_from=rn`
    let res = await fetch(url)
    let resJSON = await res.json()
    return resJSON
}

async function starDouCurrAPI(){
    try {
        let resJSON = await starDouCurr()
        if(resJSON.status_code === 0){
            return new ResultAPI(0, {}, resJSON)
        }else{
            return new ResultAPI(10003, {}, resJSON)
        }       
    } catch (error) {
        return CatchHandle(__dirname, __filename, error)
    }
}

async function starDouHistoryAPI(ctx){
    try {
        const edition_uid = ctx.query.edition_uid

        if(!edition_uid){return new ResultAPI(10000)}

        let resJSON = await starDouHistory(edition_uid)
        if(resJSON.status_code === 0){
            return new ResultAPI(0, {}, resJSON)
        }else{
            return new ResultAPI(10003, {}, resJSON)
        }       
    } catch (error) {
        return CatchHandle(__dirname, __filename, error)
    }
}

module.exports = {
    starDouCurrAPI: starDouCurrAPI,
    starDouHistoryAPI: starDouHistoryAPI
}